package com.gzhu.knowledgeAdmin.service.impl;

import com.gzhu.knowledgeAdmin.common.dto.CategoryAddRequest;
import com.gzhu.knowledgeAdmin.common.vo.SceneVO;
import com.gzhu.knowledgeAdmin.entity.Category;
import com.gzhu.knowledgeAdmin.repository.CategoryRepository;
import com.gzhu.knowledgeAdmin.service.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryRepository categoryRepository;

    @Override
    public boolean addCategory(CategoryAddRequest categoryAddRequest) {

        if (StringUtils.isBlank(categoryAddRequest.getCategory())) {
            //为空,说明添加分类(一级菜单)
            Category category = new Category();
            category.setCategory("");
            category.setValue(categoryAddRequest.getValue());
            categoryRepository.save(category);
        } else {
            //不为空,添加二级菜单
            Category category = new Category();
            category.setCategory(categoryAddRequest.getCategory());
            category.setValue(categoryAddRequest.getValue());
            categoryRepository.save(category);
        }

        return true;
    }

    @Override
    public List<String> getCategory() {
        List<Category> categoryList = categoryRepository.getCategoryByCategory("");

        return categoryList.stream().map(Category::getValue).collect(Collectors.toList());
    }

    @Override
    public List<SceneVO> getScene(String category) {
        List<Category> categoryList = categoryRepository.getCategoryByCategory(category);

        ArrayList<SceneVO> resultList = new ArrayList<>();

        for (Category scene : categoryList) {
            SceneVO sceneVO = new SceneVO();
            sceneVO.setCategory(scene.getCategory());
            sceneVO.setValue(scene.getValue());
            resultList.add(sceneVO);
        }

        return resultList;
    }
}
